using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

using vs.Core;

namespace Forum
{
    public partial class topic_edit : System.Web.UI.UserControl
    {
        private bool HaveRight(int topicID)
        {
            engine_forum engine = new engine_forum();
            string forumID = engine.ForumID2(topicID);
            if (engine.IsMOD(forumID)) return true;
            return false;
        }

        public string GetForumID_by_Topic(int topicID)
        {
            string strSQL = "SELECT ForumID FROM FORUM_Topic"
                + " WHERE (TopicID = @TopicID)";
            DataParameter p = new DataParameter("@TopicID", topicID);
            object o = DataFactory.ExecuteScalar(strSQL, p);
            if (o != null) return o.ToString();
            return "";
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    string topicID = Request.QueryString["ID"];
                    string strSQL = "SELECT * FROM FORUM_Topic WHERE TopicID=@TopicID";
                    DataParameter p = new DataParameter("@TopicID", topicID);
                    DataTable dt = DataFactory.SelectTable(strSQL, p);

                    strSQL = "SELECT ForumID, ForumName FROM FORUM_Forum";
                    DataTable dtForum = DataFactory.SelectTable(strSQL);
                    ddlForum.DataSource = dtForum;
                    ddlForum.DataTextField = "ForumName";
                    ddlForum.DataValueField = "ForumID";
                    ddlForum.DataBind();

                    if (dt.Rows.Count > 0)
                    {
                        txtTopicName.Text = dt.Rows[0]["TopicName"].ToString();
                        chkAlwayTop.Checked = Convert.ToBoolean(dt.Rows[0]["AlwayTop"]);
                        chkLocked.Checked = Convert.ToBoolean(dt.Rows[0]["Locked"]);
                        ddlForum.SelectedValue = dt.Rows[0]["ForumID"].ToString();
                    }
                    btnDelete.Attributes["onclick"] = "if(!confirm('Ban co chac chan xoa?')) {return false;};";
                    this.ViewState["url"] = Request.UrlReferrer.PathAndQuery;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int topicID = Convert.ToInt32(Request.QueryString["ID"]);
                engine_forum engine = new engine_forum();
                engine.DeleteTopic(topicID);
                string url = this.ViewState["url"].ToString();
                Response.Redirect(url);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int topicID = Convert.ToInt32(Request.QueryString["ID"]);
                string forumID = GetForumID_by_Topic(topicID);

                string strSQL = "UPDATE FORUM_Topic SET "
                    + " TopicName=@TopicName, AlwayTop=@AlwayTop, "
                    + " Locked=@Locked, ForumID=@ForumID  "
                    + " WHERE TopicID=@TopicID";
                DataParameter[] p = new DataParameter[5];
                p[0] = new DataParameter("@TopicName", txtTopicName.Text.Trim());
                p[1] = new DataParameter("@AlwayTop", chkAlwayTop.Checked);
                p[2] = new DataParameter("@Locked", chkLocked.Checked);
                p[3] = new DataParameter("@ForumID", ddlForum.SelectedValue);
                p[4] = new DataParameter("@TopicID", topicID);
                DataFactory.Execute(strSQL, p);

                string url = this.ViewState["url"].ToString();
                Response.Redirect(url);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}